#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main() {
    Mat image = imread("../../../src/cre/scripts/temp/image.jpg"); // 读取图像
    if (image.empty()) {
        cout << "Cannot read image file" << endl;
        return -1;
    }

    double angle = 30; // 旋转角度
    Point2f center(image.cols / 2.0, image.rows / 2.0); // 旋转中心
    Mat rotated = getRotationMatrix2D(center, angle, 1.0); // 获取旋转矩阵
    Rect2f bbox = RotatedRect(Point2f(), image.size(), angle).boundingRect2f(); // 获取旋转后的图像大小
    rotated.at<double>(0, 2) += bbox.width / 2.0 - image.cols / 2.0; // 调整旋转矩阵
    rotated.at<double>(1, 2) += bbox.height / 2.0 - image.rows / 2.0; // 调整旋转矩阵
    Mat rotated_image;
    warpAffine(image, rotated_image, rotated, bbox.size()); // 进行图像旋转

    namedWindow("Original Image", WINDOW_NORMAL);
    namedWindow("Rotated Image", WINDOW_NORMAL);

    imshow("Original Image", image);
    imshow("Rotated Image", rotated_image);

    waitKey(0);
    return 0;
}